package com.controller;

import com.alibaba.fastjson.JSONObject;
import com.common.contant.RestResponse;
import com.dto.BrandDTO;

import com.service.BrandService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: sijunqiang
 * @Date: 2020/5/18 20:33
 * @Description: 品牌控制层
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BrandController {

    private final BrandService brandService;


    /**
     * <P>分页查询品牌</P>
     *
     * @param page   当前页
     * @param rows   每页显示的条数
     * @param key    关键搜索
     * @param sortBy 排序字段
     * @param desc   是否降序
     * @return
     */
    @GetMapping(value = "brand/page")
    public RestResponse queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", defaultValue = "false") Boolean desc) {
        return RestResponse.success(brandService.queryBrandByPage(page, rows, key, sortBy, desc));
    }


    /**
     * 保存品牌
     */

    @PostMapping(value = "save/brand")
    public RestResponse saveBrand(@RequestBody BrandDTO brandDTO, @RequestParam("cids") List<Long> cids) {
        log.info(">>>>>>>>>保存品牌请求参数: brand:[{}], 分类的id：[{}]>>>>>>>>>>", JSONObject.toJSONString(brandDTO), JSONObject.toJSONString(cids));
        brandService.saveBrand(brandDTO, cids);
        return RestResponse.success(HttpStatus.CREATED);
    }


    /**
     * 根据品牌的id 查询商品类目信息
     *
     * @param id 品牌的id
     * @return
     */
    @GetMapping(value = "/category/of/brand")
    public RestResponse queryByBrandId(@RequestParam(value = "id") long id) {
        log.info(">>>>打印根据品牌的id获取商品类目请求参数:[{}]>>>>", id);
        RestResponse restResponse = brandService.queryByBrandId(id);
        return  restResponse;

    }
}
